﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ActorController : MonoBehaviour
{
    public IUserInput playerInput;   
    public GameObject model;    
    public float walkSpeed = 2.4f;    

    [SerializeField]
    private Animator animator;
    private Vector3 movingVec;
    private Vector3 thrustVec;
    private Vector3 deltaPos;
    private Rigidbody2D rigidbody;
    
    private bool canAttack;    

    private void Awake()
    {
        playerInput = gameObject.GetComponent<IUserInput>();
        animator = model.GetComponent<Animator>();
        rigidbody = gameObject.GetComponent<Rigidbody2D>();
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }            

    public void DoUpdate(RecInput recInput) {
        animator.SetFloat("vec_x", recInput.vec.x * (recInput.isRun ? 2.0f : 1.0f));
        animator.SetFloat("vec_y", recInput.vec.z * (recInput.isRun ? 2.0f : 1.0f));
        movingVec = recInput.vec * recInput.mag * walkSpeed * (recInput.isRun ? 2.0f : 1.0f);        
        rigidbody.velocity = new Vector3(movingVec.x, movingVec.z);
    }

    private void FixedUpdate()
    {
        
    }

    public bool CheckState(string stateName, string layerName = "Base Layer") {
        int layerIndex = animator.GetLayerIndex(layerName);
        return animator.GetCurrentAnimatorStateInfo(layerIndex).IsName(stateName);
    }

    public bool CheckStateTag(string tagName, string layerName = "Base Layer")
    {
        int layerIndex = animator.GetLayerIndex(layerName);
        return animator.GetCurrentAnimatorStateInfo(layerIndex).IsTag(tagName);
    }
    

    void OnJumpExit()
    {
        //playerInput.enabledInput = true;
        //lockPlanner = false;
    }                   

    void OnUpdateRM(object _deltaPos) {
        if (CheckState("attack1hC")) {           
            deltaPos += (Vector3)_deltaPos;
        }        
    }

    public void IssueTrigger(string name) {
        animator.SetTrigger(name);
    }

    public void OnHitEnter() {
        Debug.Log("enter hit");
        playerInput.enabledInput
            = false;
    }
}
